import axios from 'axios'
import { Notify, Toast } from 'vant';
import router from '../router';
export function request(config){
    const instance=axios.create({
        baseURL:'https://api.shop.eduwork.cn',
        timeout:5000
    })
    // 请求拦截
instance.interceptors.request.use(function (config) {
    const token=window.localStorage.getItem('token')
    if(token){
      config.headers.Authorization='Bearer'+token
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });
// 响应拦截
instance.interceptors.response.use(function (response) {
    return response.data?response.data:response;
  }, function (error) {
    if(error.response.status=='401'){
      Toast.fail('请先登录')
     setTimeout(()=>{
      router.push({path:'/login'})
     },1000)
    }
    // console.log( error.response.data);
    Notify(error.response.data.errors[Object.keys(error.response.data.errors)[0]][0])
  });
  return instance(config)
}
